from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
import base64

key = get_random_bytes(16)  # 128-bit key
data = "Hello, PyCryptodome!"

# 加密
cipher = AES.new(key, AES.MODE_CBC)
iv = cipher.iv
ciphertext = cipher.encrypt(pad(data.encode(), AES.block_size))

# 解密
decipher = AES.new(key, AES.MODE_CBC, iv=iv)
plaintext = unpad(decipher.decrypt(ciphertext), AES.block_size)

print("密文:", base64.b64encode(ciphertext).decode())
print("原文:", plaintext.decode())
